package cn.ickck.law_paper_final.utils;

import java.math.BigDecimal;

/**
 * @Author: ck
 * @Description:   四舍五入
 * @Date: Create in 15:13 2021/1/27
 */
public class RoundNoOfUtil {

    public static String RoundNoOf(String str, int scale) {
        try {
            // 输入精度小于0则抛出异常
            if (scale < 0) {
                throw new IllegalArgumentException("The scale must be a positive integer or zero");
            }

            // 取得数值
            BigDecimal b = new BigDecimal(str);
            // 取得数值1
            BigDecimal one = new BigDecimal("1");
            // 原始值除以1，保留scale位小数，进行四舍五入
            return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return str;

    }
}
